<!-- 托幼机构工作人员 -->
<template>
	<view style="padding: 15px;">
		<form @submit="formSubmit">
			<view class="uni-common-mt">
				<view class="uni-form-item uni-column user-input">
					<view class="title">姓名</view>
					<view class="input">
						<input class="uni-input" name="name" v-model="formData.name" maxlength="5"
							placeholder="请输入姓名" />
					</view>
				</view>
				<view class="fenjie"></view>
				<view class="uni-form-item uni-column user-input">
					<view class="title">性别</view>
					<view class="input">
						<radio-group name="gender" @change="bingGenderChange">
							<label>
								<radio value="1" /><text>男</text>
							</label>
							<text style="margin-right: 10px;"></text>
							<label>
								<radio value="2" /><text>女</text>
							</label>
						</radio-group>
					</view>
				</view>
				<view class="fenjie"></view>
				<view class="uni-form-item uni-column user-input">
					<view class="title">年龄</view>
					<view class="input">
						<input class="uni-input" name="age" type="number" v-model="formData.age" placeholder="请输入年龄" />
					</view>
				</view>
				<view class="fenjie"></view>
				<view class="uni-form-item uni-column user-input">
					<view class="title">婚否</view>
					<view class="input">
						<radio-group name="married" @change="bingMarriedChange">
							<label>
								<radio value="已婚" /><text>已婚</text>
							</label>
							<text style="margin-right: 10px;"></text>
							<label>
								<radio value="未婚" /><text>未婚</text>
							</label>
						</radio-group>
					</view>
				</view>
				<view class="fenjie"></view>
				<view class="uni-form-item uni-column user-input">
					<view class="title">工作单位</view>
					<view class="input">
						<input class="uni-input" name="company" v-model="formData.company" maxlength="100"
							placeholder="请输入工作单位" />
					</view>
				</view>
				<view class="fenjie"></view>
				<view class="uni-form-item uni-column user-input">
					<view class="title">岗位（职称）</view>
					<view class="input">
						<input class="uni-input" name="position" v-model="formData.position" maxlength="100"
							placeholder="请输入岗位（职称）" />
					</view>
				</view>
				<view class="fenjie"></view>
				<view class="uni-form-item uni-column user-input">
					<view class="title">民族</view>
					<view class="input">
						<input class="uni-input" name="ethnicity" v-model="formData.ethnicity" maxlength="100"
							placeholder="请输入民族" />
					</view>
				</view>
				<view class="fenjie"></view>
				<view class="uni-form-item uni-column user-input">
					<view class="title">身份证号码</view>
					<view class="input">
						<input class="uni-input" name="idcard" type="idcard" v-model="formData.idcard" maxlength="18"
							placeholder="请输入身份证号码" />
					</view>
				</view>
				<view class="fenjie"></view>
				<view class="uni-form-item uni-column user-input">
					<view class="title">电话</view>
					<view class="input">
						<input class="uni-input" name="phone" type="number" v-model="formData.phone" maxlength="11"
							placeholder="请输入电话" />
					</view>
				</view>
			</view>
			<button style="margin-top: 10px;" form-type="submit">提交预约</button>
		</form>
		
		<u-overlay :show="overlayShow"></u-overlay>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations,
		mapActions
	} from 'vuex'
	import graceChecker from "../../common/graceChecker.js"
	import WXBizDataCrypt from "../../common/WXBizDataCrypt.js"
	import {getCurrentDay} from '../../common/dateUtil.js'
	export default {
		data() {
			return {
				overlayShow: false,
				formData: {
					openid: '',
					cardid: '',
					name: '',
					gender: '',
					age: '',
					married: '',
					company: '',
					position: '',
					ethnicity: '',
					idcard: '',
					phone: '',
					checkup_package_id: ''
				},
				detail: {},
				tijianid: '',
			}
		},
		onLoad(option) {
			const eventChannel = this.getOpenerEventChannel();
			eventChannel.on('detail', (data) => {
				this.detail = data.data
			})
			this.tijianid = option.tijianid
			console.log(this.tijianid)
		},
		methods: {
			formSubmit(e) {
			
				let openid = uni.getStorageSync('openid')
				let currentUserCard = uni.getStorageSync('current_user_card')
				
			
				var rule = [
				];
				
				if (!this.formData.name) {
					uni.showToast({
						title: '请输入姓名',
						icon: "none"
					});
					return
				}
				if (!this.formData.gender) {
					uni.showToast({
						title: '请输入性别',
						icon: "none"
					});
					return
				}
				if (!this.formData.age) {
					uni.showToast({
						title: '请输入年龄',
						icon: "none"
					});
					return
				}
				if (!this.formData.married) {
					uni.showToast({
						title: '请选择婚否',
						icon: "none"
					});
					return
				}
				if (!this.formData.company) {
					uni.showToast({
						title: '请输入工作单位',
						icon: "none"
					});
					return
				}
				if (!this.formData.position) {
					uni.showToast({
						title: '请输入岗位（职称）',
						icon: "none"
					});
					return
				}
				if (!this.formData.ethnicity) {
					uni.showToast({
						title: '请输入民族',
						icon: "none"
					});
					return
				}
				if (!this.formData.idcard) {
					uni.showToast({
						title: '请输入身份证号',
						icon: "none"
					});
					return
				}
				if (!this.formData.phone) {
					uni.showToast({
						title: '请输入电话',
						icon: "none"
					});
					return
				}
			
				this.overlayShow = true
			
				//进行表单检查
				var formData = e.detail.value;
				var checkRes = graceChecker.check(formData, rule);
			
				if (checkRes) {
					let that = this
					this.$app._post_form2("entry/wxapp/apicheckup.postinfo2", {
						"openid": openid,
						"cardid": "",
						"form": {
							"name": this.formData.name,
							"gender": this.formData.gender,
							"age": this.formData.age,
							"married": this.formData.married,
							"company": this.formData.company,
							"position": this.formData.position,
							"ethnicity": this.formData.ethnicity,
							"idcard": this.formData.idcard,
							"phone": this.formData.phone,
							"checkup_package_id": this.tijianid
						}
					}, (res) => {
						this.$app._post_form("entry/wxapp/tijian.addtijian", {
							content: JSON.stringify(res.data),
							money: this.detail.price,
							// hid: '8',
							// bm_id: '8',
							// ifpay: '0',
							yy_time: getCurrentDay(),
							tid: this.tijianid,
							openid: openid,
							addproject: '',
							j_id: currentUserCard.j_id
						}, (res) => {
							let tijianid = res.id
							this.$app._post_form("entry/wxapp/tijian.paytijianorder", {
								openid: openid,
								z_tw_money: this.detail.price,
								// z_tw_money: 0.01,
								orders: res.ordernums
							}, (res) => {
								console.log(res)
								this.overlayShow = false
								let wx_data_package = JSON.parse(res.wx_data_package)
								let orderInfo = {
									"timeStamp": wx_data_package.timestamp,
									"nonceStr": wx_data_package.noncestr,
									"package": wx_data_package.package,
									"signType": wx_data_package.signType,
									"paySign": wx_data_package.sign,
								}
								uni.getProvider({
									service: 'payment',
									success(res) {
										console.log(res)
										if (res.provider.indexOf('wxpay') > -1) {
											console.log('开始支付')
											uni.requestPayment({
												"provider": "wxpay", //固定值为"wxpay"
												"timeStamp": orderInfo.timeStamp,
												"nonceStr": orderInfo.nonceStr,
												"package": orderInfo.package,
												"signType": orderInfo.signType,
												"paySign": orderInfo.paySign,
												success: (res) => {
													console.log(res)
													if (res.errMsg == 'requestPayment:ok') {
														console.log("支付成功");
														that.$app._post_form("entry/wxapp/tijian.paysucc", {
															"openid": openid,
															"id": tijianid
														}, (res) => {
															console.log(res)
															uni.navigateTo({
																url: '/pages/global/successMessage?msg=预约成功，请前往个人中心-我的体检中查看'
															})
														})
														
													}
												},
												fail: (err) => {
													console.log('支付失败:' + JSON.stringify(err));
												}
											});
										}
									}
								})
							})
						})
					})
				} else {
					uni.showToast({
						title: graceChecker.error,
						icon: "none"
					});
				}
			},
			
			bingGenderChange: function(e) {
				this.formData.gender = e.detail.value
			},
			bingMarriedChange: function(e) {
				this.formData.married = e.detail.value
			}
		}
	}
</script>

<style lang="scss" scoped>
	.user-input {
		width: 335px;
		margin: 20px auto;
		display: flex;
	
		.title {
			width: 120px;
		}
	
		.input {
			width: 280px;
			margin-left: 20px;
		}
	}
	
	.fenjie {
		height: 1px;
		background: #ddd;
		margin: 0 auto;
	}
	
	.up-button {
		width: 100%;
		height: 40px;
		background: #0096FB;
		border-radius: 4px;
		font-size: 16px;
		line-height: 40px;
		color: white;
		margin-top: 30px;
	}
</style>